/**
 * 
 */
package net.sanative.kalendar.version_1_0;

import java.util.Calendar;
import java.util.Date;

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.TimeZone;

/**
 * @author Jeff
 * 
 */
public class Timezone {

	public static final String VERSION = "1.0RC6";
	private String dateFormat = "yyyy-MM-dd HH:mm:ss.S";
	private DateFormat df;
	private com.ibm.icu.util.TimeZone tz;

	public Timezone(String tzID) {
		tz = TimeZone.getTimeZone(tzID);
	}
	
	public String fromUTC(String date) {
		return parse(date, 1);
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public com.ibm.icu.util.TimeZone getTz() {
		return tz;
	}

	public String[] getTimezones() {
		return TimeZone.getAvailableIDs();
	}
	
	public String[] getTimezones(String countryCode) {
		return TimeZone.getAvailableIDs(countryCode);
	}
	
	private String parse(String inDate, int multiplier) {
		df = new SimpleDateFormat(getDateFormat());
		Calendar date = Calendar.getInstance();
		try {
			date.setTime((Date) df.parse(inDate));
		} catch (Exception e) {
		}
		int offset = tz.getOffset(date.get(Calendar.ERA),
						date.get(Calendar.YEAR),
						date.get(Calendar.MONTH),
						date.get(Calendar.DATE),
						date.get(Calendar.DAY_OF_WEEK),
						(date.get(Calendar.HOUR_OF_DAY) * 3600000) + (date.get(Calendar.MINUTE) * 60000) + date.get(Calendar.MILLISECOND));
		date.add(Calendar.MILLISECOND, multiplier * offset);

		return df.format(date.getTime());
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

	public String toUTC(String date) {
		return parse(date, -1);
	}
}
